package me.ezitku.base.extensions

import com.ug_project.system.*
import me.ezitku.base.BaseApplication.Companion.context
import me.ezitku.base.R
import java.text.SimpleDateFormat
import java.util.*

/**
 *ۋاقىتنى فورماتلاش
 * ۋاقىت فورماتى چوقۇم yyyy-MM-dd HH:mm:ss بولىشى كېرەك
 */
fun String.format(): String {
    //بۈگۈنكى كۈننىڭ ئىسمىغا ئېرىشىش
    fun getWeek(date: Calendar): String {
        var result = ""
        var weeks = context.resources.getStringArray(R.array.day_week)
        weeks.indices
                .filter { it == date.get(Calendar.DAY_OF_WEEK) }
                .forEach { result = weeks[it] }
        return result

    }

    var date = try {
        toCalendar(defaultPattern)
    } catch (e: Exception) {
        try {
            toCalendar("yyyy-MM-dd HH:mm")
        } catch (e: Exception) {
            try {
                toCalendar(defaultPattern)
            } catch (e: Exception) {
                try {
                    toTimeFromTimesTampStirng()
                } catch (e: Exception) {
                    return "فورماتلانمىدى"
                }
            }

        }

    }

    var year = date.year
    var month = date.month
    var day = date.day
    var hour = date.hour
    var minute = date.minute

    var resultYear = ""
    var resultMonth = ""
    var resultDay = ""

    var resultHour = ""
    var resultMinute = ""


    var result = ""

    if (year < nowYear) {
        result = date.toString("yyyy-MM-dd HH:mm")
    } else {
        if (month == nowMonth && day == nowDay) {
            result += "بۈگۈن\n"
            result += "$hour:$minute"

        } else {
            result += getWeek(date) + "\n"
            result += "$month-$day"
        }
    }
    return result

}

//نۆۋەتتىكى بۈگۈنمۇ ئەمەسمۇ
fun Calendar.isToday(): Boolean {
    if ((this.year == nowYear) &&
            (this.month == nowMonth) &&
            (this.day == nowDay)) {
        return true
    }
    return false
}


//جاۋا long time نى date String گە ئايلاندۇرۇش
fun String.longToDate(default: String = defaultPattern): String {
    return SimpleDateFormat(default).format(Date(this))
}

fun String.toLongTime(pattern:String = defaultPattern): Long {
    return toDate(pattern).time
}

